package com.codingke.codingbookstore.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.codingke.codingbookstore.R;
import com.codingke.codingbookstore.adapter.AddressListItemAdapter;
import com.codingke.codingbookstore.vo.Address;
import com.codingke.codingbookstore.vo.User;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.FindListener;

public class SelectAddressActivity extends BaseActivity implements OnItemClickListener {

    private ListView listView2_address;
    private AddressListItemAdapter addressListItemAdapter;
    private ArrayList<Address> addresses = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select_address);
        listView2_address = (ListView) findViewById(R.id.listView2_address);
        listView2_address.setOnItemClickListener(this);
        addressListItemAdapter = new AddressListItemAdapter(this, addresses, R.layout.address_list_item);
        listView2_address.setAdapter(addressListItemAdapter);


    }

    @Override
    protected void onResume() {
        super.onResume();
        loadAddressData();
    }

    /**
     * 加载用户地址列表
     */
    private void loadAddressData() {
        BmobQuery<Address> query = new BmobQuery<>();
        query.addWhereEqualTo("userId", BmobUser.getCurrentUser(this, User.class).getObjectId());
        query.order("-isDefault");//排序isDefault降序
        query.findObjects(this, new FindListener<Address>() {
            @Override
            public void onSuccess(List<Address> list) {
                addresses = (ArrayList<Address>) list;
                addressListItemAdapter.setAddresses(addresses);
                addressListItemAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(int i, String s) {
                showToast("查找地址失败");
            }
        });
    }


    /**
     * 管理按钮事件
     *
     * @param view
     */
    public void addressManagerClick(View view) {
        Intent intent = new Intent(this, AddressManagerActivity.class);
        intent.putExtra("addresses", addresses);
        startActivity(intent);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        view.setSelected(true);
        Address address = addresses.get(0);
        address.setIsDefault(false);
        address.update(this);
        address = addresses.get(position);
        address.setIsDefault(true);
        address.update(this);
        back(address);
    }

    /**
     * 重写返回方法,回传列表的第一个地址对象
     * @param view
     */
    @Override
    public void backClick(View view) {
        back(addresses.get(0));
    }

    private void back(Address address) {
        Intent data = getIntent();
        data.putExtra("address", address);
        setResult(RESULT_OK, data);
        finish();
    }
}
